package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.feign.clients.WmmediaClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.ApAuthDto;
import com.heima.model.user.dtos.ApAuthPassOrFailDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserAuthMapper;
import com.heima.user.service.ApUserService;
import com.heima.user.service.IApUserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * app端用户认证,后台管理
 */
@Service
public class ApUserAuthServiceImpl extends ServiceImpl<ApUserAuthMapper, ApUserRealname> implements IApUserAuthService {
    @Autowired
    private ApUserService apUserService;
    @Autowired
    private WmmediaClient wmmediaClient;
    /**
     * 后台管理端查询用户认证列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByPage(ApAuthDto dto) {
        //校验请求参数是否存在
        if (dto.getPage() == null || dto.getSize() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //构造分页查询条件
        Page<ApUserRealname> page = new Page<>(dto.getPage(),dto.getSize());
        //判断请求参数中有无状态查询
        //构造条件查询条件
        QueryWrapper<ApUserRealname> queryWrapper = new QueryWrapper<>();
        if (dto.getStatus() != null){
            queryWrapper.eq("status",dto.getStatus());
        }
        //调用分页查询方法
        Page<ApUserRealname> pageInfo = this.page(page, queryWrapper);
        //封装分页查询结果集
        PageResponseResult responseResult = new PageResponseResult();
        responseResult.setCurrentPage(dto.getPage());
        responseResult.setSize(dto.getSize());
        responseResult.setTotal((int) pageInfo.getTotal());
        responseResult.setData(pageInfo.getRecords());
        return responseResult;
    }

    /**
     * 用户审核通过
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authPass(ApAuthPassOrFailDto dto) {
        //校验请求参数是否存在
        if (dto.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id查询用户认证表
        ApUserRealname apUserRealname = this.getById(dto.getId());
        if (apUserRealname == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //对用户认证表的审核状态进行修改
        apUserRealname.setStatus((short) 9);
        //修改用户认证表
        this.updateById(apUserRealname);
        //审核通过后,根据用户id查询用户表,获取app用户信息
        ApUser apUser = apUserService.getById(apUserRealname.getUserId());
        //向自媒体用户表中新增一条数据
        //调用远程服务
        ResponseResult responseResult = wmmediaClient.saveWmUser(apUser);
        //校验远程服务返回结果
        if (!responseResult.getCode().equals(200)){
            return ResponseResult.errorResult(200,"用户审核通过,新增自媒体用户失败");
        }
        return ResponseResult.okResult(200,"操作成功");
    }

    /**
     * 用户认证---驳回
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(ApAuthPassOrFailDto dto) {
        //校验请求参数是否存在
        if (dto.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id查询用户认证表
        ApUserRealname apUserRealname = this.getById(dto.getId());
        if (apUserRealname == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //对用户认证表的审核状态进行修改
        apUserRealname.setStatus((short)2);
        apUserRealname.setReason(dto.getMsg());
        //修改用户认证表
        this.updateById(apUserRealname);
        return ResponseResult.okResult(200,"操作成功");
    }
}
